import * as path from "path";
import cheerio from "cheerio";
import { useURL } from "./utils/check";
import { startRequest } from "./request";
import { generateFile } from "./write-file";
import { parseConfig, siteContent, SiteContentValue } from "./types";

const scrape = require("website-scraper");

export function runApp(
	url: string,
	publicDir: string,
	relative: boolean = false
) {
	startRequest(url, ({ body }) => {
		const $ = cheerio.load(body);

		parseConfig.forEach(({ tag, name }) => {
			$(tag).each((index, element) => {
				const value = element.attribs[name];
				if (value) {
					const URL = new useURL(value);
					const pushValue = {
						cdn: URL.isCDN(),
						originValue: value,
						url: URL.getUrl(url),
						file: URL.getFile(publicDir),
					};
					siteContent[tag].push(pushValue);

					$(tag)[index].attribs[name] = pushValue.url;
				}
			});
		});

		generateFile("index.html", publicDir, relative ? $.html() : body);
		generateFile("source.json", publicDir, JSON.stringify(siteContent));
		Object.entries(siteContent).forEach(([_key, item]) => {
			(item as SiteContentValue[]).forEach((value) => {
				if (!value.cdn) {
					generateFile(value.file, publicDir);
					contentDownload(value, publicDir);
				}
			});
		});
	});
}

function contentDownload(value: SiteContentValue, publicDir: string) {
	const { file, url } = value;
	scrape({
		urls: [url],
		directory: path.dirname(file),
	})
		.then(() => {
			console.log(`download file from ${url}`);
		})
		.catch((err: Error) => {});
}
